package com.gxuwz.medical.domain.institution;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.gxuwz.medical.database.DbUtil;

/**
 * 农合机构管理类
 * 
 * @author 演示
 * 
 */
public class Institution {

	public String getAreapcode() {
		return areapcode;
	}

	public void setAreapcode(String areapcode) {
		this.areapcode = areapcode;
	}

	public String getAreacode() {
		return areacode;
	}

	public void setAreacode(String areacode) {
		this.areacode = areacode;
	}

	public String getAgencode() {
		return agencode;
	}

	public void setAgencode(String agencode) {
		this.agencode = agencode;
	}

	public String getAgenname() {
		return agenname;
	}

	public void setAgenname(String agenname) {
		this.agenname = agenname;
	}

	public int getGrade() {
		return grade;
	}

	public void setGrade(int grade) {
		this.grade = grade;
	}

	public Institution(String areacode) throws Exception {
		this.areacode = areacode;
		if(!"0".equals(areacode)){
			getInst(areacode);
		}
	}
	

	public Institution(String areacode, String agencode, String agenname,
			int grade) {
		super();
		this.areacode = areacode;
		this.agencode = agencode;
		this.agenname = agenname;
		this.grade = grade;
	}

	/**
	 * 行政区域编码，类标识符
	 */
	private String areacode;
	/**
	 * 经办机构编码
	 */
	private String agencode;
	/**
	 * 经办机构名称
	 */
	private String agenname;
	/**
	 * 经办机构级别，区别于行政区域级别，1表示县级农合办，2表示镇级农合点
	 */
	private int grade;

	private Institution parent;

	private String areapcode;

	public Institution() {

	}

	public Institution getParent() throws Exception {
		if (this.parent == null) {
			this.parent = new Institution();
		}
		return this.parent;
	}

	public void setParent(Institution parent) {
		this.parent = parent;
	}

	/**
	 * 添加农合经办点
	 * 
	 * @param areacode
	 * @param agencode
	 * @param agenname
	 * @param grade
	 */
	public void addInst(String areacode, String agencode, String agenname,
			int grade) throws Exception {
		Connection conn = null;
		try {
			// 初始化属性
			this.areacode = areacode;
			this.agencode = agencode;
			this.agenname = agenname;
			this.grade = grade;
			// 保存导数据库
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			// 调用插入数据库私有方法
			this.saveToDB(conn);
			conn.commit();
		} catch (Exception e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}

	}

	public void getInst(String areacode) throws Exception {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs =null;
		try {
			conn = DbUtil.getConn();
			pstmt = conn.prepareStatement("select * from t_institution where areacode=?");
			int index = 1;
			pstmt.setString(index, this.areacode);
			rs = pstmt.executeQuery();
			if(rs.next()){
				this.areacode = rs.getString("areacode");
				this.agenname = rs.getString("agenname");
				this.agencode = rs.getString("agencode");
				this.grade    = rs.getInt("grade");
			}
			
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(rs,pstmt,conn);
		}
	}

	/**
	 * 插入记录
	 * 
	 * @param connection
	 * @throws SQLException
	 */
	private void saveToDB(Connection connection) throws SQLException {
		PreparedStatement pstmt = null;
		try {

			StringBuffer sqlBuff = new StringBuffer();
			sqlBuff.append("insert into t_institution(areacode,agencode,agenname,grade)");
			sqlBuff.append("values(?,?,?,?)");
			String sql = sqlBuff.toString();
			pstmt = connection.prepareStatement(sql);
			// 依次设置动态参数的值，注意顺序
			int index = 1;
			pstmt.setString(index++, this.areacode);// 行政区域编码
			pstmt.setString(index++, this.agencode);
			pstmt.setString(index++, this.agenname);
			pstmt.setInt(index, this.grade);
			// 以executeUpdate()执行SQL
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			// 只关闭PrepareStatement资源
			if (pstmt != null) {
				pstmt.close();
			}

		}

	}

	public List<Institution> children() throws Exception {
		List<Institution> instList = new ArrayList<Institution>();
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DbUtil.getConn();
			pstmt = conn
					.prepareStatement("select * from t_institution where areacode like'?%' and grade=?");
			int index = 1;
			pstmt.setString(index++, this.areacode);
			pstmt.setInt(index, this.grade + 1);
			rs = pstmt.executeQuery();

			while (rs.next()) {
				Institution inst = new Institution();
				inst.setAreacode(rs.getString("areaid"));
				inst.setAreapcode(rs.getString("areapcode"));
				inst.setAgencode(rs.getString("agencode"));
				inst.setAgenname(rs.getString("agenname"));
				inst.setGrade(rs.getInt("grade"));
				instList.add(inst);

			}

		} catch (SQLException e) {

			throw e;
		} finally {
			DbUtil.close(rs, pstmt, conn);
		}

		return instList;
	}

	public void delInst(String areacode) throws SQLException {
		Connection conn = null;
		this.areacode = areacode;
		try {
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			deleteFromDB(conn);
			conn.commit();
		} catch (SQLException e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}

	}

	protected void deleteFromDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"delete from t_institution where areacode=?");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			pstmt.setString(1, this.areacode);

			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("Failed to delete record from table !", e);
		} finally {
			DbUtil.close(pstmt);
		}

	}

	public void editInst(String agencode, String agenname)
			throws SQLException {
		Connection conn = null;
		this.agencode = agencode;
		this.agenname = agenname;
		try {
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			editToDB(conn);
			conn.commit();
		} catch (SQLException e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}

	}

	/**
	 * 
	 * @param conn
	 * @throws SQLException
	 */
	private void editToDB(Connection conn) throws SQLException {

		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"update t_institution t set t.agencode=? , t.agenname=? where t.areacode=?");
			pstmt = conn.prepareStatement(sqlBuff.toString());

			pstmt.setString(1, this.agencode);
			pstmt.setString(2, this.agenname);
			pstmt.setString(3, this.areacode);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("Failed to update t_area !", e);
		} finally {
			DbUtil.close(pstmt);
		}
	}

}
